package com.landian.demo.redis.service.impl;

import com.google.common.collect.Lists;
import com.landian.demo.redis.service.ProductService;
import com.landian.demo.redis.vo.ProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author caojl
 * @date: 2018-06-21
 * @time: 14:04
 */
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Override
    public List<ProductVO> list() {
        return Lists.newArrayList(ProductVO.builder().id("101").name("产品1").build(),
                ProductVO.builder().id("102").name("产品2").build());
    }

    // 缓存key为name的数据到缓存productcache中
    @Cacheable(value = "productcache", key = "#p0")
    @Override
    public ProductVO findProduct(String name) {
        System.out.println("无缓存时执行下面代码，Time：" + System.currentTimeMillis());
        // 模拟从持久层获取数据
        ProductVO productVO = ProductVO.builder().name(name).build();
        return productVO;
    }

}
